/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.view;


import ohos.agp.components.LayoutScatter;
import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.global.configuration.Configuration;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.solidxml.Theme;
import ohos.global.resource.solidxml.TypedAttribute;

import java.util.ArrayList;
import java.util.List;

/**
 * A ContextWrapper that allows you to modify the theme from what is in the
 * wrapped context.
 * <p>
 * source code is copied from ContextThemeWrapper, the difference is setTheme() method,
 * SceneContextThemeWrapper setTheme will not modify Ability's theme
 *
 */
public class SceneContextThemeWrapper extends AbilityContext {
    private int mThemeResource;
    private Theme mTheme;
    private Configuration mOverrideConfiguration;
    private ResourceManager mResources;
    private boolean mIsThemeFromAbility;

    public SceneContextThemeWrapper(Context base, int themeResId) {
        super(base);
        mThemeResource = themeResId;
        if (mThemeResource == 0) {
            throw new IllegalArgumentException("themeResId can't be zero");
        }
    }

    public SceneContextThemeWrapper(Context base, Theme theme) {
        super(base);
        mIsThemeFromAbility = true;
    }

    @Override
    public void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
    }

    public void applyOverrideConfiguration(Configuration overrideConfiguration) {
        if (mResources != null) {
            throw new IllegalStateException(
                    "getResources() or getAssets() has already been called");
        }
        if (mOverrideConfiguration != null) {
            throw new IllegalStateException("Override configuration has already been set");
        }
        mOverrideConfiguration = new Configuration(overrideConfiguration);
    }

    public Configuration getOverrideConfiguration() {
        return mOverrideConfiguration;
    }

    @Override
    public ResourceManager getResourceManager() {
        return getResourcesInternal();
    }

    private ResourceManager getResourcesInternal() {
        if (mResources == null) {
            if (mOverrideConfiguration == null) {
                mResources = super.getResourceManager();
            }
        }
        return mResources;
    }

    @Override
    public void setTheme(int themeId) {
        if (mThemeResource != themeId) {
            mThemeResource = themeId;
            if (mIsThemeFromAbility) {
                //reset
                mTheme = null;
                mResources = null;
            }
        }
    }

    public int getThemeResId() {
        return mThemeResource;
    }

    @Override
    public Theme getTheme() {
        if (mTheme != null) {
            return mTheme;
        }
        return mTheme;
    }

    protected void onApplyThemeResource(Theme theme) {
        theme.set(theme);
    }

    private void initializeTheme() {
        final boolean first = mTheme == null;
        if (first) {
            List<TypedAttribute.AttrData> data=new ArrayList<>();
            mTheme = getResourceManager().createTheme(data);
            Theme theme = getContext().getTheme();
            if (theme != null) {
                mTheme.set(theme);
            }
        }
        onApplyThemeResource(mTheme);
    }

}